// pages/habit_new/habit_new.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    detail: "",
    time_way: "any",
    start_time: "00:00",
    stop_time: "23:59",
    to_show_dates: ["1", "2", "3", "4", "5", "6", "7"],
    select_weekday: [0, 0, 0, 0, 0, 0, 0],
    btn_disabled: false
  },

  submit: function(e) {
    var that = this

    if (that.data.name == "" || that.data.detail == "") {
      wx.showModal({
        title: '哎呀～',
        content: '有的选项没填呢！',
      })
      return
    } else {
      var flag = 0;
      for (var i = 0; i < 7; i++) {
        if (that.data.select_weekday[i]) {
          flag = 1
        }
      }

      if (flag == 0) {
        wx.showModal({
          title: '哎呀～',
          content: '有的选项没填呢！',
        })
        return
      }
    }

    that.setData({
      btn_disabled: true
    })
    wx.showLoading({
      title: '正在提交',
    })
    console.log("新建微习惯")
    wx.login({
      success: function(res_code) {
        if (res_code.code) {

          wx.request({
            url: getApp().globalData.my_server + "/minihabit/new_minihabit",
            data: {
              code: res_code.code,
              name: that.data.name,
              detail: that.data.detail,
              time_way: that.data.time_way,
              start_time: that.data.start_time,
              stop_time: that.data.stop_time,
              select_weekday: that.data.select_weekday
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(res) {
              console.log(res)
              if (res.data.error_no == 2) {
                wx.hideLoading()
                that.setData({
                  btn_disabled: false
                })
                wx.showModal({
                  title: '哎呀～',
                  content: res.data.error_msg,
                  success: function(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (res.data.error_no == 0) {
                wx.hideLoading()

                wx.navigateBack({
                  delta: 1
                })

              }
            },
            fail: function(res) {
              wx.hideLoading()
              that.setData({
                btn_disabled: false
              })
              wx.showModal({
                title: '哎呀～',
                content: '创建微习惯失败，请重新创建',
                success: function(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              console.log("fail!", res)
            },
            complete: function(res) {

            }
          })


        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });

  },

  select_weekday: function(e) {
    var index = e.currentTarget.dataset.index
    var select_weekday = this.data.select_weekday
    select_weekday[index] = !select_weekday[index]

    this.setData({
      select_weekday: select_weekday
    })
  },

  bindStartTimeChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      start_time: e.detail.value
    })
  },

  bindStopTimeChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      stop_time: e.detail.value
    })
  },

  radioChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      time_way: e.detail.value
    })
  },

  name_input: function(e) {
    this.setData({
      name: e.detail.value
    })
  },

  detail_input: function(e) {
    this.setData({
      detail: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})